import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/auth'
// import { component } from 'vue/types/umd'
// import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

// 防止重复点击报错
const routerRePush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
  return routerRePush.call(this, location).catch(error => error)
}


const routes = [
  {
    path:'/',
    name:'',
    // component: Layout,
    component: ()=>import('@/layout/index'),
    children:[
      {
        path:'',
        name:'Home',
        component: ()=>import('@/views/index')
      },
      {
        // 关于我们
        path:'/about',
        name:'',
        component: ()=>import('@/views/about')
      },
      {
        // 新闻通知
        path:'/new',
        name:'',
        component: ()=>import('@/views/new'),
      },
      {
        // 新闻通知
        path:'/new-list',
        name:'',
        component: ()=>import('@/views/new-list')
      },
      {
        // 新闻通知
        path:'/new-detail/:id',
        name:'',
        component: ()=>import('@/views/new-detail')
      },
      {
        // 图片相册
        path:'/pic-detail/:id',
        name:'',
        component: ()=>import('@/views/pic-detail')
      },
      {
        // 资格培训
        path:'/training',
        name:'',
        component: ()=>import('@/views/training')
      },
      {
        // 视频照片
        path:'/media',
        name:'',
        component: ()=>import('@/views/media')
      },
      {
        // 联系合作
        path:'/cooperate',
        name:'',
        component: ()=>import('@/views/cooperate')
      },
      {
        // 查询
        path:'/search',
        name:'',
        component: ()=>import('@/views/search')
      },
      {
        path: '/user',
        meta: { isAuth: true },
        beforeEnter: (to, from, next) => {
          if (to.meta.isAuth) { //判断是否需要授权
            if (getToken()) {
              next('/user/userInfo')  //放行
            } else {
              next('/login')
            }
          } else {
              next()  //放行
          }
        },
        component: ()=>import('@/views/user'),
        children: [
          // 基本信息
          {
            path: 'userInfo',
            name: 'userInfo',
            component: ()=>import('@/components/User/userInfo'),
          },
          // 认证信息
          {
            path: 'certification',
            name: 'certification',
            component: ()=>import('@/components/User/certification'),
          },
          // 队伍运动员管理
          {
            path: 'athlete',
            name: 'athlete',
            component: ()=>import('@/components/User/athlete'),
          },
          // 家长运动员管理
          {
            path: 'athletejiazhang',
            name: 'athletejiazhang',
            component: ()=>import('@/components/User/athletejiazhang'),
          },
          // 裁判管理
          {
            path: 'referee',
            name: 'referee',
            component: ()=>import('@/components/User/referee'),
          },
          // 教练管理
          {
            path: 'coach',
            name: 'coach',
            component: ()=>import('@/components/User/coach'),
          },
          // 领队管理
          {
            path: 'teamleader',
            name: 'teamleader',
            component: ()=>import('@/components/User/teamleader'),
          },
          // 人员管理
          {
            path: 'teamother',
            name: 'teamother',
            component: ()=>import('@/components/User/teamother'),
          },
          // 比赛列表
          {
            path: 'competition',
            name: 'competition',
            component: ()=>import('@/components/User/competition'),
          },
          // 比赛报名
          {
            path: 'choosecompetition/:id',
            name: 'choosecompetition',
            component: ()=>import('@/components/User/choosecompetition'),
          },
          // 项目列表
          {
            path: 'projectlist',
            name: 'projectlist',
            component: ()=>import('@/components/User/projectlist'),
          },
          // 培训列表
          {
            path: 'training',
            name: 'training',
            component: ()=>import('@/components/User/training'),
          },
          // 培训报名
          {
            path: 'choosetraining/:id',
            name: 'choosetraining',
            component: ()=>import('@/components/User/choosetraining'),
          },
          // 培训列表
          {
            path: 'traininglist',
            name: 'traininglist',
            component: ()=>import('@/components/User/traininglist'),
          },
          // 打分列表
          {
            path: 'test',
            name: 'test',
            component: ()=>import('@/components/User/test'),
          }
        ]
      }
    ]
  },
  {
    path:'/login',
    name:'',
    component: ()=>import('@/views/login')
  },
  {
    path:'/register',
    name:'',
    component: ()=>import('@/views/register')
  },
  {
    path:'/login-score',
    name:'',
    component: ()=>import('@/views/login-score')
  },
  {
    path:'/score',
    name:'',
    component: ()=>import('@/views/score'),
    children:[
    {
      // 普通裁判
      path:'normalreferee',
      name:'',
      component: ()=>import('@/components/Score/normalreferee')
    },
    {
      // 裁判长
      path:'headreferee',
      name:'',
      component: ()=>import('@/components/Score/headreferee')
    },
    {
      name: '404',
      path: '/404',
      component: () => import('@/views/notFound.vue')
    },
    {
      path: '*',    // 此处需特别注意至于最底部
      redirect: '/404'
    }
  ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.beforeEach((to, from, next) => {
//   // 判断是否进入 /user 路径
//   if (to.path === '/user') {
//     // 将路径重定向为 /user/setting
//     next({path: '/user/aaa'})
//   } else {
//     // 不需要进入 /user 路径
//     next()
//   }
// })

export default router
